import Taro from '@tarojs/taro';

// 通用请求处理函数
const request = async (options) => {
  try {
    const response = await Taro.request(options);
    
    // 检查响应状态码
    if (response.statusCode !== 200) {
      throw new Error(`HTTP error! status: ${response.statusCode}`);
    }
    
    // 解析响应数据
    const { code, message, data } = response.data;
    
    // 根据 code 处理响应
    if (code === 200) {
      return { success: true, data, message };
    } else {
      return { success: false, message, errorDetail: response.data.errorDetail };
    }
  } catch (error) {
    console.error('Request failed:', error);
    return { success: false, message: '请求失败，请稍后重试', errorDetail: error.message };
  }
};

export default request;
